/**
 * list 集合
 * @module List
 * @date ###### Mon Oct 16 22:35:09 CST 2017
 * @author jinglf000
 */

class List {
	constructor(arr) {
		let size = 0;
		const items = {};
		arr.forEach((element) => {
			if (items[element] === undefined) {
				items[element] = element;
				size += 1;
			}
		}, this);
		this.size = size;
		this.items = items;
	}
	/**
	 * 向集合中添加元素
	 * @param {*} item
	 * @return list集合
	 */
	add(element) {
		if (this.items[element] === undefined) {
			this.size += 1;
			this.items[element] = element;
		}
	}
	/**
	 * 向集合中移除元素
	 * @param {*} element
	 * @returns {boolean} 成功为true失败false
	 */
	remove(element) {
		if (this.items[element] !== undefined) {
			delete this.items[element];
			this.size -= 1;
			return true;
		}
		return false;
	}
	/**
	 * 清空集合
	 */
	clear() {
		this.size = 0;
		this.items = {};
	}
	/**
	 * 对集合内容进行遍历
	 * @param {function} fn
	 * @desc fn中有三个参数index，value，this
	 */
	entries(fn) {
		if (typeof fn === 'function') {
			Object.keys().forEach((index, item) => {
				fn.call(this, index, item, this.items);
			});
		}
	}
}

module.exports = List;
